﻿// object
Proggy.CorePropertyTypes.Clts = function () {

    // variable
    var _areaId = undefined;
    var _limit = undefined;
    var _min = undefined;
    var _textEl = undefined;
    var _area = undefined;
    var _isTextArea = false;
    var _rmn = undefined;
    var _minRmn = undefined;

    //
    var bindTextLimit = function () {

        // check lmit
        if (_limit > 0) {

            // get area
            _area = jQuery("div#" + _areaId);

            // get textel
            _textEl = _area.find("input:first");

            // check
            if (_textEl.size() == 0) {
                _textEl = _area.find("textarea:first");
                _isTextArea = true;
            };

            // get remainder span
            _rmn = _area.find("span.clts-limit-rmn:first");
            _minRmn = _area.find("span.clts-min-rmn:first");

            // create function
            var checkCharsFunction = function () {
                // change limittext
                _rmn.text(_limit - _textEl.val().length);

                // do min sum
                var minSum = _min - _textEl.val().length;

                // get min text
                var minText = minSum < 1 ? 0 : minSum;

                // change min
                _minRmn.text(minText);
            };

            // bind
            _textEl.blur(function () { checkCharsFunction(); });
            _textEl.keyup(function () { checkCharsFunction(); });
            _textEl.focus(function () { checkCharsFunction(); });

        };
    };

    //
    this.Init = function (areaId, limit, min) {

        // set variables
        _areaId = areaId;
        _limit = limit;
        _min = min;

        // bind text limit
        bindTextLimit();
    };
};